<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="true">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="SurveyUricAcidForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="创建日期" v-bind="validateInfos.createTime" id="SurveyUricAcidForm-createTime" name="createTime">
								<a-date-picker placeholder="请选择创建日期"  v-model:value="formData.createTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="尿酸值" v-bind="validateInfos.uricAcid" id="SurveyUricAcidForm-uricAcid" name="uricAcid">
								<a-input-number v-model:value="formData.uricAcid" placeholder="请输入尿酸值" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="疼痛位置" v-bind="validateInfos.painLocation" id="SurveyUricAcidForm-painLocation" name="painLocation">
								<a-input v-model:value="formData.painLocation" placeholder="请输入疼痛位置"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="疼痛持续时间" v-bind="validateInfos.painDuration" id="SurveyUricAcidForm-painDuration" name="painDuration">
								<a-input v-model:value="formData.painDuration" placeholder="请输入疼痛持续时间"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="痛风发作周期" v-bind="validateInfos.goutCycle" id="SurveyUricAcidForm-goutCycle" name="goutCycle">
								<a-input v-model:value="formData.goutCycle" placeholder="请输入痛风发作周期"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="当前用药" v-bind="validateInfos.currentMedication" id="SurveyUricAcidForm-currentMedication" name="currentMedication">
								<a-input v-model:value="formData.currentMedication" placeholder="请输入当前用药"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="性别" v-bind="validateInfos.gender" id="SurveyUricAcidForm-gender" name="gender">
								<a-input v-model:value="formData.gender" placeholder="请输入性别"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="体重" v-bind="validateInfos.weight" id="SurveyUricAcidForm-weight" name="weight">
								<a-input-number v-model:value="formData.weight" placeholder="请输入体重" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="年龄" v-bind="validateInfos.age" id="SurveyUricAcidForm-age" name="age">
								<a-input-number v-model:value="formData.age" placeholder="请输入年龄" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="其他疾病" v-bind="validateInfos.otherDiseases" id="SurveyUricAcidForm-otherDiseases" name="otherDiseases">
								<a-input v-model:value="formData.otherDiseases" placeholder="请输入其他疾病"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="手机号" v-bind="validateInfos.phone" id="SurveyUricAcidForm-phone" name="phone">
								<a-input v-model:value="formData.phone" placeholder="请输入手机号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
<!--						<a-col :span="24">-->
<!--							<a-form-item label="回复" v-bind="validateInfos.reply" id="SurveyUricAcidForm-reply" name="reply">-->
<!--								<a-textarea v-model:value="formData.reply" :rows="4" placeholder="请输入回复" />-->
<!--							</a-form-item>-->
<!--						</a-col>-->
<!--						<a-col :span="24">-->
<!--							<a-form-item label="回复状态" v-bind="validateInfos.replyStatus" id="SurveyUricAcidForm-replyStatus" name="replyStatus">-->
<!--								<a-input-number v-model:value="formData.replyStatus" placeholder="请输入回复状态" style="width: 100%" />-->
<!--							</a-form-item>-->
<!--						</a-col>-->
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
    <JFormContainer >
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="SurveyUricAcidForm">
          <a-row>
            <a-col :span="24">
              <a-form-item  label="回复" v-bind="validateInfos.reply" id="SurveyUricAcidForm-reply" name="reply">
                <a-textarea v-model:value="formData.reply" :rows="4" placeholder="请输入" />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../SurveyUricAcid.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    createTime: '',   
    uricAcid: undefined,
    painLocation: '',   
    painDuration: '',   
    goutCycle: '',   
    currentMedication: '',   
    gender: '',   
    weight: undefined,
    age: undefined,
    otherDiseases: '',   
    phone: '',   
    reply: '',
    replyStatus:1
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    uricAcid: [{ required: true, message: '请输入尿酸值!'},],
    painLocation: [{ required: true, message: '请输入疼痛位置!'},],
    painDuration: [{ required: true, message: '请输入疼痛持续时间!'},],
    goutCycle: [{ required: true, message: '请输入痛风发作周期!'},],
    currentMedication: [{ required: true, message: '请输入当前用药!'},],
    gender: [{ required: true, message: '请输入性别!'},],
    weight: [{ required: true, message: '请输入体重!'},],
    age: [{ required: true, message: '请输入年龄!'},],
    otherDiseases: [{ required: true, message: '请输入其他疾病!'},],
    phone: [{ required: true, message: '请输入手机号!'},],
    reply: [{ required: true, message: '请输入回复!'},],
    replyStatus: [{ required: true, message: '请输入回复状态!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    if (formData.reply) {
      formData.replyStatus = 1
    }
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
